<?php
require_once(dirname(__FILE__) . '/PortalUtil.class.php');

class Log extends PortalUtil {
	public $tbl = Constant::tbl_wifi_log;

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function _where($request) {
		$uid_s = $request['uid_s'];
		$status_s = $request['status_s'];
	
		$where = " WHERE 1=1";
		
		if (!empty($uid_s)) {
			$where .= " AND uid = '" . $uid_s . "'";
		}
		
		if (!empty($status_s)) {
			$where .= " AND status = '" . $status_s . "'";
		}
	
		// $where .= $this->_where_default($request);
		// $where = $this->_where_aa($where);
	
		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function layout($controller, $request) {
		$this->pretreat($controller);
		
		if (empty($_SESSION['username'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
		
		$request['uid_s'] = $_SESSION['uid'];
		
		$this->nav['home'] = true;
		
		$this->findPara($controller, $request);
		
		// $this->orderby_s = "id";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$pager_option = array (
			'db' => $db,
			'sql' => $sql,
			'PageSize' => 8,
			'CurrentPageID' => $request['p'],
			'numItems' => $request['n']
		);
		
		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();
		
		if ($pager->number > 0) {
		
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
		
				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
		
		$controller->display($request, 'log/index');
		return true;
	}
	
	function online($controller, $request) {
		$this->pretreat($controller);
	
		if (empty($_SESSION['username'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
	
		$request['uid_s'] = $_SESSION['uid'];
		$request['status_s'] = 1;
	
		$this->nav['home'] = true;
	
		$this->findPara($controller, $request);
	
		// $this->orderby_s = "id";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
	
		$pager_option = array (
				'db' => $db,
				'sql' => $sql,
				'PageSize' => 8,
				'CurrentPageID' => $request['p'],
				'numItems' => $request['n']
		);
	
		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();
	
		if ($pager->number > 0) {
	
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
	
				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
	
		$controller->display($request, 'log/online');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		
	
		$row['mac_t'] = $this->_mac_t($row['mac']);
		
		return $row;
	}
	
	function offline($controller, $request) {
		$this->pretreat($controller);
		
		if (empty($_SESSION['username'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
		
		$id = $request['id'];
		$username = $_SESSION['username'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET token = '', status = '2', modifytime = now() WHERE username = '%s' AND id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $username, $id);
		debug($sql);
		$db->query($sql);
		
		$this->online($controller, $request);
		return true;
	}
	
	function logout($controller, $request) {
		$this->pretreat($controller);
	
		$token = $_SESSION['token'];
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '2' WHERE token = '%s'";
		$sql = sprintf($fmt, $this->tbl, $token);
		$db->query($sql);
	
		@session_start();
		$_SESSION = array();
	
		$controller->assign('username', '');
	
		$controller->display($request, 'index');
		return true;
	}
	
	function _login($request, $user) {
		$office = $user['office'];
		$dept = $user['dept'];
		$emp = $user['emp'];
		
		$uid = $user['id'];
		
		$token = $request['token'];
		$gw_id = $request['gw_id'];
		
		$ip = $request['ip'];
		$mac = $request['mac'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '2' WHERE status = '1' AND mac = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mac);
		$db->query($sql);
		
		$fmt = "INSERT INTO %s(office, dept, emp, uid, token, gw_id, ip, mac, createtime, modifytime) 
				VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', NOW(), NOW())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $uid, $token, $gw_id, $ip, $mac);
		debug($sql);
		$db->query($sql);
		
		$this->_mac($request, $user);
		
		$this->_c1($request, $user);
	}
	
	function _c1($request, $user) {
		
		$uid = $user['id'];
		$c1 = $user['c1'];
		
		$dd = date("Y-m-d", strtotime("-3 day"));
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE uid = '%s' AND status = '1' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $uid);
		$result = $db->query($sql);
		$i = 0;
		while ($row = $db->fetch_array($result)) {
			
			if ($i++ < $c1) {
				continue;
			}
			
			$fmt = "UPDATE %s SET status = '2', modifytime = NOW() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			$db->query($sql);
		}
	}
	
	function _mac($request, $user) {
		require_once(dirname(__FILE__) . '/Mac.class.php');
		$aa = new Mac();
		
		$aa->_update2($request, $user);
	}
	
	function _logout($request, $user) {
		
		$office = $user['office'];
		$dept = $user['dept'];
		$emp = $user['emp'];
		
		$uid = $user['id'];
		
		$token = $request['token'];
		$gw_id = $request['gw_id'];
		
		$ip = $request['ip'];
		$mac = $request['mac'];
		
		$token = $request['token'];
		$incoming = $request['incoming'];
		$outgoing = $request['outgoing'];
		
		$bytes = $incoming + $outgoing;
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE token = '%s'";
		$sql = sprintf($fmt, $this->tbl, $token);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET incoming = '%s', outgoing = '%s', bytes = '%s', status = '2', modifytime = NOW() WHERE token = '%s'";
			$sql = sprintf($fmt, $this->tbl, $incoming, $outgoing, $bytes, $token);
		} else {
			$fmt = "INSERT INTO %s(office, dept, emp, uid, token, gw_id, ip, mac, incoming, outgoing, bytes, status, createtime, modifytime) 
				VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '2', NOW(), NOW())";
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $uid, $token, $gw_id, $ip, $mac, $incoming, $outgoing, $bytes);
		}
		debug($sql);
		$db->query($sql);
	}
	
	function _counters($request, $user) {
	
		$token = $request['token'];
		$incoming = $request['incoming'];
		$outgoing = $request['outgoing'];
	
		$bytes = $incoming + $outgoing;
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET incoming = '%s', outgoing = '%s', bytes = '%s', modifytime = NOW() WHERE token = '%s'";
		$sql = sprintf($fmt, $this->tbl, $incoming, $outgoing, $bytes, $token);
		debug($sql);
		$db->query($sql);
	}
	
	function _valid($request, $user) {
		
		$token = $request['token'];
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE token = '%s'";
		$sql = sprintf($fmt, $this->tbl, $token);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				return true;
			}
		}
		
		return false;
	}
}
?>
